﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Xml.XPath;
using System.IO;
using System.Diagnostics;

namespace workshop3
{
    public partial class Formlogin : Form
    {
        public Formlogin()
        {
            InitializeComponent();
            if (File.Exists("Account.xml"))
            {
                
            }
            else
            {
                MessageBox.Show("This is the first time you use this system.\nNow please input your username and password to create your account.");
            }
            lbltip2.Hide();
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void txtusername_TextChanged(object sender, EventArgs e)
        {

        }

        private void btnlogin_Click(object sender, EventArgs e)
        {
            if (File.Exists("Account.xml"))
            {
                String path = "//Account";
                XmlDocument doc = new XmlDocument();
                doc.Load("Account.xml");
                XmlNode node = doc.SelectSingleNode(path);
                if(node.ChildNodes[0].InnerText == txtusername.Text && node.ChildNodes[1].InnerText == txtpassword.Text)//check name and password
                {
                    MessageBox.Show("login success!");
                    Formmanagement management = new Formmanagement();
                    management.Show();
                    this.Hide();
                }
            }
            else//if there is no account file
            {
                int check;
                ClassCreateXML.CreateAccountXmlFile(txtusername.Text,txtpassword.Text);
                check = ClassCreateXML.checktheaccount();
                if (check == 1)
                {
                    MessageBox.Show("Create account successfully!\n");
                }
            }
        }

        private void btnforgetpassword_Click(object sender, EventArgs e)
        {
            //ClassExcelToXml.ExcelToXml();
            Process pro = new Process();
            pro.StartInfo.FileName = "Account.xml";//open the account file to show the username and password 
            pro.StartInfo.Verb = "Open";
            pro.StartInfo.CreateNoWindow = true;
            pro.Start();
        }

        private void Formlogin_Load(object sender, EventArgs e)
        {

        }

        private void btnforgetpassword_MouseMove(object sender, MouseEventArgs e)
        {
            lbltip2.Show();
        }

        private void btnforgetpassword_MouseLeave(object sender, EventArgs e)
        {
            lbltip2.Hide();
        }
    }
}
